home *** CD-ROM | disk | FTP | other *** search
- /* NameRegistryDisplayMain.c */
- /*
- * NameRegistryDisplayMain.c
- * Copyright © 1993-95 Apple Computer Inc. All rights reserved.
- * Edit History
- * 1.0a3 (94.12.23) Release for A5 DDK
- * 1.0a4 (94.12.30) Format "reg" property.
- * 1.0a5 (95.01.19) Fix bug: "save doesn't write anything" Correctly
- * dispose of the primary and secondary lists.
- * 1.0b1 (95.03.23) Fix bug: it is possible to have two independent registry
- * entries with the same name. Fix the sort algorithm so
- * they aren't smooshed together.
- */
- #define EXTERN /* Allocate variables */
-
- #include "DisplayNameRegistry.h"
- #if (defined(__powerc) || defined(powerc)) && defined(__MWERKS__) == 0
- /*
- * Power PC does not automatically define the QuickDraw globals. This is because
- * only "application" code-fragments need these globals, and this cannot be
- * determined before the code fragment is constructed.
- */
- QDGlobals qd; /* This is not automatically defined on PowerPC */
- #endif
- Boolean gSaveAllElements = FALSE;
-
- /*
- * These enum's define items in the various menus.
- */
- enum AppleMenu {
- kAppleAbout = 1
- };
- enum { /* File Menu */
- kFileSaveAs = 1,
- kFileUnused1,
- kFilePageSetup,
- kFilePrint,
- kFileUnused2,
- kFileQuit
- };
- enum EditMenu {
- kEditUndo = 1,
- kEditUnused,
- kEditCut,
- kEditCopy,
- kEditPaste,
- kEditClear
- };
- enum OptionMenu {
- kOptionSortByName = 1,
- kOptionSortByProperty,
- kOptionUnused1,
- kOptionRefreshDisplay,
- kOptionSetFontInfo
- };
-
- Boolean gInForeground;
- long gSleepTime;
-
- /*
- * Local function prototypes.
- */
- void main(void);
- void ProcessThisEvent(
- const EventRecord *eventRecordPtr,
- long *sleepTimePtr
- );
- void ApplicationEventLoop(void);
- void DoMouseEvent(
- const EventRecord *eventRecordPtr
- );
- void DoCommand(
- WindowPtr activeWindow,
- long menuChoice
- );
- void AdjustMenus(void);
- void InitMacintosh(void);
- void InitApplication(void);
- void AdjustEditMenu(
- Boolean isDeskAcc
- );
- static Boolean IsOurWindow(
- WindowPtr theWindow
- );
- void DoPageSetup(void);
- void DoAbout(void);
- pascal OSErr MyAEOpenAppHandlerFunc(
- long refCon
- );
- pascal OSErr MyAEQuitAppHandlerFunc(
- long refCon
- );
- pascal void MyAEUnknownHandlerMsgFunc(
- ConstStr255Param messageText
- );
- pascal Boolean MyAEIdleProc(
- const EventRecord *theEventPtr,
- long *sleepTime,
- RgnHandle *mouseRgn
- );
- RoutineDescriptor gAEIdleUPP =
- BUILD_ROUTINE_DESCRIPTOR(uppAEIdleProcInfo, MyAEIdleProc);
-
- /*
- * main
- * The application main program. This is a limited program for the twist-down
- * sample; it is not intended as a model for a complete Macintosh program.
- */
- void
- main()
- {
- OSErr status;
-
- InitMacintosh();
- InitApplication();
- status = InitializeAppleEvents(
- MyAEOpenAppHandlerFunc,
- NULL, /* No open doc handler */
- NULL, /* No print doc handler */
- MyAEQuitAppHandlerFunc,
- NULL, /* No DoScript callback */
- MyAEUnknownHandlerMsgFunc,
- 0 /* No refCon */
- );
- if (status != noErr) {
- if (status == gestaltUndefSelectorErr) /* Huh? no AppleEvents? */
- status = MakeNameRegistryBrowserWindow();
- if (status == noErr)
- FatalError(status, "\pCan't initialize AppleEvents");
- }
- gInForeground = TRUE;
- gSleepTime = 0;
- InitCursor();
- while (gQuitNow == FALSE) {
- if (gUpdateMenusNeeded)
- AdjustMenus();
- WaitNextEvent(everyEvent, &gEventRecord, gSleepTime, NULL);
- gAECoreGlobals.currentEventIsAppleEvent = FALSE;
- ProcessThisEvent(&gEventRecord, &gSleepTime);
- }
- }
-
- pascal OSErr
- MyAEOpenAppHandlerFunc(
- long refCon
- )
- {
- OSErr status;
-
- UNUSED(refCon);
- status = MakeNameRegistryBrowserWindow();
- gUpdateMenusNeeded = TRUE;
- return (status);
- }
-
- pascal OSErr
- MyAEQuitAppHandlerFunc(
- long refCon
- )
- {
- UNUSED(refCon);
- gQuitNow = TRUE;
- return (noErr);
- }
-
- pascal void
- MyAEUnknownHandlerMsgFunc(
- ConstStr255Param messageText
- )
- {
- NonFatalError(errAEHandlerNotFound, messageText);
- }
-
- void
- ProcessThisEvent(
- const EventRecord *eventRecordPtr,
- long *sleepTimePtr
- )
- {
- OSErr status;
- long menuChoice;
- register WindowPtr theWindow;
- GrafPtr savePort;
- Boolean isActivating;
- #define EVENT (*eventRecordPtr)
-
- theWindow = FrontWindow();
- switch (EVENT.what) {
- case nullEvent:
- break;
- case keyDown:
- case autoKey:
- if ((EVENT.message & charCodeMask) == '.'
- && (EVENT.modifiers & cmdKey) != 0) {
- FlushEvents(keyDown | autoKey, 0);
- gQuitNow = TRUE;
- }
- else if ((EVENT.modifiers & cmdKey) != 0) {
- if (EVENT.what == keyDown) {
- menuChoice = MenuKey(EVENT.message & charCodeMask);
- if (HiWord(menuChoice) != 0 && IsOurWindow(theWindow))
- DoCommand(theWindow, menuChoice);
- else if (IsOurWindow(theWindow))
- DoWindowKeyDown((BrowserPtr) theWindow);
- else {
- SysBeep(10);
- }
- }
- }
- else if (IsOurWindow(theWindow))
- DoWindowKeyDown((BrowserPtr) theWindow);
- else {
- SysBeep(10);
- }
- break;
- case mouseDown:
- DoMouseEvent(eventRecordPtr);
- break;
- case updateEvt:
- theWindow = (WindowPtr) EVENT.message;
- GetPort(&savePort);
- SetPort(theWindow);
- BeginUpdate(theWindow);
- EraseRect(&theWindow->portRect);
- DrawGrowIcon(theWindow);
- DrawControls(theWindow);
- if (IsOurWindow(theWindow))
- UpdateBrowserWindow((BrowserPtr) theWindow, theWindow->visRgn);
- EndUpdate(theWindow);
- SetPort(savePort);
- break;
- case activateEvt:
- theWindow = (WindowPtr) EVENT.message;
- isActivating = ((EVENT.modifiers & activeFlag) != 0);
- goto activateEvent;
- break;
- case kHighLevelEvent:
- status = AEProcessAppleEvent(&EVENT);
- switch (status) {
- case userCanceledErr:
- case errAEEventNotHandled:
- case noErr:
- break;
- default:
- NonFatalError(status, "\pAppleEvent handler error");
- break;
- }
- break;
- case osEvt:
- switch (((unsigned long) EVENT.message) >> 24) {
- case mouseMovedMessage:
- break;
- case suspendResumeMessage:
- isActivating = ((EVENT.message & 0x01) != 0);
- activateEvent: if (isActivating) {
- /*
- * Activate this window. Activate events define theWindow from
- * the event record, while suspend/resume uses the pre-set
- * FrontWindow value.
- */
- SelectWindow(theWindow);
- SetPort(theWindow);
- (void) TEFromScrap();
- }
- if (IsOurWindow(theWindow)) {
- ActivateBrowser(
- (BrowserPtr) theWindow,
- isActivating
- );
- /*
- * Globalize the current window for the debugger's convenience.
- */
- if (isActivating)
- gCurrentBrowserPtr = (BrowserPtr) theWindow;
- }
- else {
- /* Desk accessory or what? */
- }
- gInForeground = isActivating;
- *sleepTimePtr = (gInForeground) ? 6 : 60;
- gUpdateMenusNeeded = TRUE;
- break;
- }
- break;
- }
- #undef EVENT
- }
-
- /*
- * DoMouseEvent
- * The user clicked on something. Handle application-wide processing here, or call
- * a Browser function for specific action.
- */
- void
- DoMouseEvent(
- const EventRecord *eventRecordPtr
- )
- {
- WindowPtr theWindow;
- short whichPart;
- #define EVENT (*eventRecordPtr)
-
- whichPart = FindWindow(EVENT.where, &theWindow);
- if (theWindow == NULL)
- theWindow = FrontWindow();
- if (whichPart == inMenuBar && IsOurWindow(theWindow) == FALSE)
- theWindow = FrontWindow();
- switch (whichPart) {
- case inDesk:
- break;
- case inMenuBar:
- InitCursor();
- DoCommand(theWindow, MenuSelect(EVENT.where));
- break;
- case inDrag:
- DragWindow(theWindow, EVENT.where, &qd.screenBits.bounds);
- break;
- case inZoomIn:
- case inZoomOut:
- if (IsOurWindow(theWindow)
- && TrackBox(theWindow, EVENT.where, whichPart)) {
- DoZoomWindow(theWindow, whichPart);
- goto resizeWindow;
- }
- break;
- case inGrow:
- if (IsOurWindow(theWindow)
- && DoGrowWindow(
- theWindow,
- EVENT.where,
- kMinWindowWidth,
- kMinWindowHeight)) {
- resizeWindow: DecorateBrowserWindow((BrowserPtr) theWindow);
- }
- break;
- case inGoAway:
- if (TrackGoAway(theWindow, EVENT.where)) {
- if (IsOurWindow(theWindow)) {
- DisposeBrowser((BrowserPtr) theWindow);
- if (gOpenWindowCount <= 0)
- gQuitNow = TRUE;
- }
- else {
- SysBeep(10);
- }
- }
- break;
- case inContent:
- if (theWindow != FrontWindow())
- SelectWindow(theWindow);
- else if (IsOurWindow(theWindow)) {
- DoContentClick((BrowserPtr) theWindow, eventRecordPtr);
- }
- else {
- /* Nothing happens here */
- }
- break;
- default:
- break; /* Bogus click: ignore */
- }
- #undef EVENT
- }
-
- /*
- * DoCommand
- * Process a menu or keystroke command.
- */
- void
- DoCommand(
- WindowPtr theWindow,
- long menuChoice
- )
- {
- short menuItem;
- Str255 menuText;
- GrafPtr savePort;
- BrowserPtr browserPtr;
- Boolean oldSortByName;
-
- menuItem = LoWord(menuChoice);
- switch (HiWord(menuChoice)) {
- case MENU_Apple:
- if (menuItem == kAppleAbout)
- DoAbout();
- else {
- GetMenuItemText(gAppleMenu, menuItem, menuText);
- AdjustEditMenu(TRUE);
- GetPort(&savePort);
- OpenDeskAcc(menuText);
- SetPort(savePort);
- AdjustEditMenu(IsOurWindow(theWindow) == FALSE);
- }
- break;
- case MENU_File:
- switch (menuItem) {
- case kFileSaveAs:
- CreateOutputFile();
- break;
- case kFilePageSetup:
- DoPageSetup();
- break;
- case kFilePrint:
- if (IsOurWindow(theWindow))
- PrintBrowserWindow((BrowserPtr) theWindow);
- break;
- case kFileQuit:
- gQuitNow = TRUE;
- break;
- }
- break;
- case MENU_Edit:
- if (SystemEdit(menuItem - 1) == FALSE)
- SysBeep(10);
- break;
- case MENU_Options:
- if (IsOurWindow(theWindow)) {
- browserPtr = (BrowserPtr) theWindow;
- oldSortByName = BROWSER.sortByName;
- switch (menuItem) {
- case kOptionSortByName:
- BROWSER.sortByName = TRUE;
- break;
- case kOptionSortByProperty:
- BROWSER.sortByName = FALSE;
- break;
- case kOptionRefreshDisplay:
- DoRefreshDisplay(browserPtr);
- break;
- case kOptionSetFontInfo:
- if (AEInteractionOK((AEIdleUPP) &gAEIdleUPP)
- && SetFontInfoDialog(browserPtr))
- DoSetFontInfo(browserPtr);
- break;
- }
- if (oldSortByName != BROWSER.sortByName) {
- gUpdateMenusNeeded = TRUE;
- SortAndDisplayBrowserWindow(browserPtr);
- }
- }
- break;
- }
- HiliteMenu(0);
- }
-
- /*
- * AdjustMenus
- * Enable/disable menu options.
- */
- void
- AdjustMenus(void)
- {
- register BrowserPtr browserPtr;
- Boolean isOurWindow;
-
- isOurWindow = IsOurWindow(FrontWindow());
- EnableItem(gAppleMenu, kAppleAbout);
- EnableItem(gFileMenu, kFileQuit);
- EnableItem(gFileMenu, kFilePageSetup);
- AdjustEditMenu(isOurWindow == FALSE);
- if (isOurWindow) {
- browserPtr = (BrowserPtr) FrontWindow();
- EnableItem(gOptionMenu, 0);
- EnableItem(gFileMenu, kFileSaveAs);
- EnableItem(gFileMenu, kFilePrint);
- CheckItem(gOptionMenu, kOptionSortByName, BROWSER.sortByName);
- CheckItem(gOptionMenu, kOptionSortByProperty, !BROWSER.sortByName);
- }
- else {
- DisableItem(gOptionMenu, 0);
- DisableItem(gFileMenu, kFileSaveAs);
- DisableItem(gFileMenu, kFilePrint);
- }
- }
-
- /*
- * AdjustEditMenu
- * Enable/disable Edit Menu options.
- */
- void
- AdjustEditMenu(
- Boolean isDeskAcc
- )
- {
- if (isDeskAcc) {
- EnableItem(gEditMenu, kEditUndo);
- EnableItem(gEditMenu, kEditCut);
- EnableItem(gEditMenu, kEditCopy);
- EnableItem(gEditMenu, kEditPaste);
- EnableItem(gEditMenu, kEditClear);
- }
- else {
- DisableItem(gEditMenu, kEditUndo);
- DisableItem(gEditMenu, kEditCut);
- DisableItem(gEditMenu, kEditCopy);
- DisableItem(gEditMenu, kEditPaste);
- DisableItem(gEditMenu, kEditClear);
- }
- }
-
- /*
- * InitMacintosh
- * Perform the normal application initialization. This must be extended for "real"
- * applications. The only thing this module does is initialize the managers.
- */
- void
- InitMacintosh(void)
- {
- int i;
-
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0);
- HNoPurge((Handle) GetCursor(watchCursor));
- SetCursor(*GetCursor(watchCursor));
- for (i = 0; i < 3; i++)
- EventAvail(everyEvent, &gEventRecord);
- }
-
- /*
- * InitApplication
- * Continue initialization.
- */
- void
- InitApplication(void)
- {
- (void) TEFromScrap();
- SetMenuBar(GetNewMBar(MBAR_MenuBar));
- gAppleMenu = GetMenuHandle(MENU_Apple);
- gFileMenu = GetMenuHandle(MENU_File);
- gEditMenu = GetMenuHandle(MENU_Edit);
- gOptionMenu = GetMenuHandle(MENU_Options);
- AppendResMenu(GetMenuHandle(MENU_Apple), 'DRVR');
- DrawMenuBar();
- SetupAnimatedCursor(ACUR_Animator);
- }
-
- /*
- * IsOurWindow
- * Return TRUE if this is a browser window.
- */
- Boolean
- IsOurWindow(
- WindowPtr theWindow
- )
- {
- if (theWindow == NULL
- || ((WindowPeek) theWindow)->windowKind != userKind)
- return (FALSE);
- else {
- return (TRUE);
- }
- }
-
- void
- pstrcpy(
- StringPtr dst,
- ConstStr255Param src
- )
- {
- BlockMoveData(src, dst, src[0] + 1);
- }
-
- void
- pstrcat(
- StringPtr dst,
- ConstStr255Param src
- )
- {
- short length;
-
- length = 255 - dst[0];
- if (length > src[0])
- length = src[0];
- BlockMoveData(&src[1], &dst[1] + dst[0], length);
- dst[0] += length;
- }
-
- /*
- * DoZoomWindow
- * Algorithm from New Inside Mac (Toolbox Essentials) 4-55
- */
- void
- DoZoomWindow(
- WindowPtr theWindow,
- short whichPart
- )
- {
- GDHandle gd;
- GDHandle gdZoom;
- GrafPtr savePort;
- Rect windowRect;
- Rect zoomRect;
- Rect intersection;
- long thisArea;
- long greatestArea;
- short windowTitleHeight;
- long response;
- Boolean hasColorQuickDraw;
- #define PEEK (*((WindowPeek) theWindow))
- #define STATE (**((WStateDataHandle) PEEK.dataHandle))
-
- GetPort(&savePort);
- SetPort(theWindow);
-
- hasColorQuickDraw = FALSE;
- if (Gestalt(gestaltQuickdrawVersion, &response) == noErr
- && response >= gestalt8BitQD)
- hasColorQuickDraw = TRUE;
- if (whichPart == inZoomOut) {
- if (hasColorQuickDraw == FALSE) {
- /*
- * This shouldn't happen on a modern system, but, if it does, just
- * force a single screen zoom.
- */
- zoomRect = qd.screenBits.bounds;
- InsetRect(&zoomRect, 4, 4);
- STATE.stdState = zoomRect;
- }
- else {
- /*
- * We have color QuickDraw. Locate the screen that contains
- * the largest area of the window and zoom to that screen.
- */
- windowRect = theWindow->portRect;
- LocalToGlobal(&topLeft(windowRect));
- LocalToGlobal(&botRight(windowRect));
- windowTitleHeight = windowRect.top
- - 1
- - (**PEEK.strucRgn).rgnBBox.top;
- windowRect.top -= windowTitleHeight;
- greatestArea = 0;
- gdZoom = NULL;
- /*
- * Look at all graphics devices and find an intersection. Then
- * select the largest intersecting screen.
- */
- for (gd = GetDeviceList(); gd != NULL; gd = GetNextDevice(gd)) {
- if (TestDeviceAttribute(gd, screenDevice)
- && TestDeviceAttribute(gd, screenActive)
- && SectRect(&windowRect, &(**gd).gdRect, &intersection)) {
- thisArea = ((long) width(intersection))
- * ((long) height(intersection));
- if (thisArea > greatestArea) {
- greatestArea = thisArea;
- gdZoom = gd;
- }
- }
- }
- /*
- * If we're zooming to the device with the menu bar,
- * allow for its height.
- */
- if (GetMainDevice() == gdZoom)
- windowTitleHeight += GetMBarHeight();
- zoomRect = (**gdZoom).gdRect;
- InsetRect(&zoomRect, 3, 3);
- zoomRect.top += windowTitleHeight;
- STATE.stdState = zoomRect;
- } /* End if color QuickDraw */
- } /* End if zoom out */
- ZoomWindow(theWindow, whichPart, (theWindow == FrontWindow()));
- /*
- * Zoom redraws the entire window.
- */
- EraseRect(&theWindow->portRect);
- InvalRect(&theWindow->portRect);
- SetPort(savePort);
- #undef PEEK
- #undef STATE
- }
-
- /*
- * DoGrowWindow
- * Algorithm from New Inside Mac (Toolbox Essentials) 4-58 (simplified)
- * We assume that the window can cover the entire screen.
- */
- Boolean
- DoGrowWindow(
- WindowPtr theWindow,
- Point startingPoint,
- short minimumWidth,
- short minimumHeight
- )
- {
- long growSize;
- Rect limitRect;
-
- limitRect.left = minimumWidth;
- limitRect.top = minimumHeight;
- limitRect.right = qd.screenBits.bounds.right;
- limitRect.bottom = qd.screenBits.bounds.bottom;
- growSize = GrowWindow(theWindow, startingPoint, &limitRect);
- if (growSize != 0) {
- SizeWindow(theWindow, LoWord(growSize), HiWord(growSize), TRUE);
- /*
- * Force a redraw of the entire window, (The correct algorithm
- * excludes the intersection of the old view rect and the new view
- * rect, but this doesn't work correctly yet.) Invalidate any prior
- * update region in any case.
- */
- EraseRect(&theWindow->portRect);
- InvalRect(&theWindow->portRect);
- }
- return (growSize != 0);
- }
-
- void
- DoPageSetup(void)
- {
- OSErr status;
-
- PrOpen();
- status = PrError();
- if (status != noErr)
- NonFatalError(status, "\pPrinting disabled");
- else {
- if (gPrintHandle == NULL) {
- gPrintHandle = (THPrint) NewHandle(sizeof (TPrint));
- if (gPrintHandle != NULL)
- PrintDefault(gPrintHandle);
- }
- if (gPrintHandle != NULL)
- (void) PrStlDialog(gPrintHandle);
- PrClose();
- }
- }
-
- void
- DoAbout(void)
- {
- GrafPtr savePort;
- DialogPtr dialog;
- short dialogItem;
-
- dialog = GetNewDialog(DLOG_About, NULL, (WindowPtr) -1L);
- if (dialog != NULL) {
- GetPort(&savePort);
- SetPort(dialog);
- ShowWindow(dialog);
- ModalDialog(NULL, &dialogItem);
- DisposeDialog(dialog);
- SetPort(savePort);
- }
- }
-
- void
- CheckError(
- OSErr errorStatus,
- ConstStr255Param errorMsg
- )
- {
- if (errorStatus != noErr && errorStatus != userCanceledErr)
- NonFatalError(errorStatus, errorMsg);
- }
-
- void
- NonFatalError(
- OSErr errorStatus,
- ConstStr255Param errorMsg
- )
- {
- if (ErrorMessage(ALRT_NonFatalError, errorStatus, errorMsg) == kOKButton)
- gQuitNow = TRUE;
- }
-
- void
- FatalError(
- OSErr errorStatus,
- ConstStr255Param errorMsg
- )
- {
- ErrorMessage(ALRT_FatalError, errorStatus, errorMsg);
- ExitToShell();
- }
-
- short
- ErrorMessage(
- short alertID,
- OSErr errorStatus,
- ConstStr255Param errorMsg
- )
- {
- Handle errorTextHdl;
- StringPtr errorTextPtr;
- Str15 errorStatusText;
- short result;
-
- if (AEInteractionOK((AEIdleUPP) &gAEIdleUPP) == FALSE)
- result = kCancelButton;
- else {
- NumToString(errorStatus, errorStatusText);
- errorTextHdl = GetResource('Estr', errorStatus);
- if (errorTextHdl != NULL) {
- HLock(errorTextHdl);
- errorTextPtr = (StringPtr) errorTextHdl;
- }
- else {
- errorTextPtr = "\pSystem Error";
- }
- ParamText(errorStatusText, errorTextPtr, errorMsg, "\p");
- InitCursor();
- result = StopAlert(alertID, NULL);
- if (errorTextHdl != NULL)
- ReleaseResource(errorTextHdl);
- }
- return (result);
- }
-
- pascal Boolean
- MyAEIdleProc(
- const EventRecord *theEventPtr,
- long *sleepTime,
- RgnHandle *mouseRgn
- )
- {
- UNUSED(mouseRgn);
- if (theEventPtr->what == kHighLevelEvent)
- return (TRUE);
- else {
- ProcessThisEvent(theEventPtr, sleepTime); /* Application specific */
- return (FALSE);
- }
- }
-